package com.example.duckshop.ui.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.duckshop.R;
import com.example.duckshop.adapter.OrderAdapter;
import com.example.duckshop.bean.OrdersVo;
import com.example.duckshop.bean.Shop;
import com.example.duckshop.ui.activity.ShopDetailActivity;
import com.example.duckshop.util.MySqliteOpenHelper;
import com.example.duckshop.util.SPUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单
 */
public class OrderFragment extends Fragment {
    MySqliteOpenHelper helper = null;
    private Activity myActivity;
    private LinearLayout llEmpty;
    private RecyclerView rvOrderList;
    public OrderAdapter mOrderAdapter;
    private List<OrdersVo> ordersVoList = new ArrayList<>();
    private Integer userId;
    private Integer userType;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        myActivity = (Activity) context;


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_order,container,false);
        helper = new MySqliteOpenHelper(myActivity);
        rvOrderList = view.findViewById(R.id.rv_order_list);
        llEmpty = view.findViewById(R.id.ll_empty);

        //获取控件
        initView();
        return view;
    }



    private void initView() {
        userId = (Integer) SPUtils.get(myActivity,SPUtils.USER_ID,0);
        userType = (Integer) SPUtils.get(myActivity,SPUtils.USER_TYPE,0);
        LinearLayoutManager layoutManager=new LinearLayoutManager(myActivity);
        //=1.2、设置为垂直排列，用setOrientation方法设置(默认为垂直布局)
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //=1.3、设置recyclerView的布局管理器
        rvOrderList.setLayoutManager(layoutManager);
        //==2、实例化适配器
        //=2.1、初始化适配器
        mOrderAdapter =new OrderAdapter();
        //=2.3、设置recyclerView的适配器
        rvOrderList.setAdapter(mOrderAdapter);
        mOrderAdapter.setItemListener(new OrderAdapter.ItemListener() {
            @Override
            public void ItemClick(OrdersVo order) {
                Shop shop = null;
                String sql = "select * from shop where id ="+order.getShopId();
                SQLiteDatabase db = helper.getWritableDatabase();
                Cursor cursor = db.rawQuery(sql,null);
                if (cursor != null && cursor.getColumnCount() > 0) {
                    while (cursor.moveToNext()) {
                        Integer dbId = cursor.getInt(0);
                        Integer dbTypeId = cursor.getInt(1);
                        String dbTitle = cursor.getString(2);
                        String dbImg = cursor.getString(3);
                        String dbContent = cursor.getString(4);
                        String dbPrice = cursor.getString(5);
                        String dbDate = cursor.getString(6);
                        shop = new Shop(dbId,dbTypeId, dbTitle, dbImg, dbContent, dbPrice,dbDate);
                    }
                }
                Intent intent = new Intent(myActivity, ShopDetailActivity.class);
                intent.putExtra("shop",shop);
                startActivity(intent);
            }




            @Override
            public void Delete(OrdersVo order) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(myActivity);
                dialog.setMessage("确认要删除该订单吗");
                dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SQLiteDatabase db = helper.getWritableDatabase();
                        String sql1 = "delete from orders where id=?";
                        db.execSQL(sql1,new String[]{String.valueOf(order.getId())});
                        db.close();
                        Toast.makeText(myActivity, "删除成功", Toast.LENGTH_SHORT).show();
                        loadData();
                    }
                });
                dialog.setNeutralButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                dialog.show();
            }
        });
        loadData();//加载数据
    }

    /**
     * 加载数据
     */
    private void loadData(){
        ordersVoList = new ArrayList<>();
        OrdersVo ordersVo = null;
        String sql = "select o.*,title,nickName from shop s,orders o,user u where s.id = o.shopId and o.userId = u.id ";
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = db.rawQuery(sql,null);
        if (cursor != null && cursor.getColumnCount() > 0) {
            while (cursor.moveToNext()) {
                Integer dbId = cursor.getInt(0);
                Integer dbUserId = cursor.getInt(1);
                Integer dbShopId = cursor.getInt(2);
                String dbAmount = cursor.getString(3);
                String dbDate = cursor.getString(4);
                String dbTitle = cursor.getString(5);
                String dbNickName = cursor.getString(6);
                ordersVo = new OrdersVo(dbId,dbUserId, dbShopId, dbAmount, dbDate, dbTitle,dbNickName);
                ordersVoList.add(ordersVo);
            }
        }

        if (ordersVoList !=null && ordersVoList.size()>0){
            rvOrderList.setVisibility(View.VISIBLE);
            llEmpty.setVisibility(View.GONE);
            mOrderAdapter.addItem(ordersVoList);
        }else {
            rvOrderList.setVisibility(View.GONE);
            llEmpty.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        loadData();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100){
            loadData();
        }
    }







}
